package com.example.demo.pojo;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

public class Tool {

    public static int randomInt(int n) {
        return ThreadLocalRandom.current().nextInt((int) Math.pow(10, n - 1), (int) Math.pow(10, n));
    }

    public static String randomString(int n) {
        int a = n / 2;
        int b = n - a;
        String strA = String.valueOf(ThreadLocalRandom.current().nextInt((int) Math.pow(10, a - 1), (int) Math.pow(10, a)));
        String strB = String.valueOf(ThreadLocalRandom.current().nextInt((int) Math.pow(10, b - 1), (int) Math.pow(10, b)));
        return strA + strB;
    }

    public static String passwordRSA(String password) {
        List<Integer> arr = findFirstPrimes(password.length());
        BigInteger big = BigInteger.ONE;
        for (int i = 0; i < password.length(); i++) {
            BigInteger temp = new BigInteger(arr.get(i) + "").pow(Integer.parseInt(String.valueOf(password.charAt(i))));
            big = big.multiply(temp);
        }
        return big.toString();
    }
    public static List<Integer> findFirstPrimes(int n) {
        List<Integer> primes = new ArrayList<>();
        int count = 0;
        int num = 2;
        while (count < n) {
            if (isPrime(num)) {
                primes.add(num);
                count++;
            }
            num++;
        }
        return primes;
    }

    public static boolean isPrime(int num) {
        if (num <= 1) {
            return false;
        }
        for (int i = 2; i <= Math.sqrt(num); i++) {
            if (num % i == 0) {
                return false;
            }
        }
        return true;
    }
}
